package com.study.rabbitmq;

import com.study.rabbitmq.config.RabbitConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

import java.io.UnsupportedEncodingException;

public class ProducerAnnotationApp {

    public static void main(String[] args) throws InterruptedException, UnsupportedEncodingException {
        AbstractApplicationContext context = new AnnotationConfigApplicationContext(RabbitConfig.class);

        RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);


        Message msg;

        MessageProperties messageProperties = MessagePropertiesBuilder.newInstance()
                .setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setContentEncoding("gbk")
                .setHeader("mykey", "myvalue")
                .build();



        //rabbitTemplate.send("exchange.anno","routingkey.annotation",msg);

        for (int i = 0; i < 1000; i++) {
            msg = MessageBuilder.withBody(("你好，rabbitmq"+i).getBytes("gbk"))
                    .andProperties(messageProperties)
                    .build();
            rabbitTemplate.send("exchange.anno","routingkey.annotation",msg);
            Thread.sleep(1000);
        }


        context.close();
    }
}
